#!/bin/bash

function see_file()
{
    home=`pwd`
    for file_name in `cat $home/filedir.txt` ; do
           if [ $file_name = $1 ]; then
              echo "0"
              return
           fi
        done
}

function find_file()
{
     find / -type f -perm -$2 2>/dev/null > $1
     write_file=`see_file $1`
     if [[ $write_file != 0 ]]; then
     echo "$1" >> filedir.txt
     fi
     echo "the path has been saved in $1!!"
}

function change_permission()
{
     home=`pwd`
     for file in `cat $home/$1` ; do
     echo "change the permission of $file:{ yes ,no ,back }?"
     read delete
     input_true=false
     while [ $input_true = false ]; do
        if [ $delete = yes ] && [ $1 = SGID_path.txt ]; then 
           chmod -s $file >> changed_SGID_file.txt
           write_file=`see_file changed_SGID_file.txt`
           if [[ $write_file != 0 ]]; then
              echo "changed_SGID_file.txt" >> filedir.txt
           fi
           echo "the permission have changed!!"
           input_true=true
        elif [ $delete = yes ] && [ $1 = SUID_path.txt ]; then 
           chmod -s $file >> changed_SUID_file.txt
           write_file=`see_file changed_SUID_file.txt`
           if [[ $write_file != 0 ]]; then
              echo "changed_SUID_file.txt" >> filedir.txt
           fi
           echo "the permission have changed!!"
           input_true=true
        elif [ $delete = no ]; then
           echo "the permission don not change!!"
           input_true=true
        elif [ $delete = back ]; then
           echo "OK ,return now!!"           
           return
        else
           echo "input error!!"
           input_true=false
           echo "please input again"
           read delete
        fi
        done
     done
}

function see_path()
{
     home=`pwd`
     for path in `cat $home/$1` ; do
     echo "$path"
     done
}

function interface()
{
     echo "###############################################"
     echo "*******please select the function number!!"
     echo "*******1.1find SGID"
     echo "*******1.2find SUID"
     echo "*******1.3find SU&GID"
     echo "*******2.1change the permission of SGID"
     echo "*******2.2change the permission of SUID"
     echo "*******2.3change the permission of SU&GID"
     echo "*******3.1see the path of SUID"
     echo "*******3.2see the path of SGID"
     echo "*******3.3see the path of SU&GID"
     echo "*******4.1see the changed file of SUID"
     echo "*******4.2see the changed file of SGID"
     echo "*******4.3see the changed file of SU&GID"
     echo "*******0.exit"
     echo "###############################################"
    
}

err_output="the file don not be find, please select other function!!"

function run_fun_change()
{
     err=`see_file $1`
       if [[ $err = 0 ]]; then 
           change_permission $1
       else
           echo "$err_output"
       fi 
}

function run_fun_see()
{
     err=`see_file $1`
       if [[ $err = 0 ]]; then 
           see_path $1
       else
           echo "$err_output"
       fi
}

home=`pwd`

touch $home/filedir.txt

while true ;

do
    interface
    read num
    case "$num" in
    0) echo "thank you and good bye!!" 
       exit 0 ;;
    1.1) find_file SGID_path.txt 4000 ;;
    1.2) find_file SUID_path.txt 2000 ;;
    1.3) find_file SUGID_path.txt 6000 ;;
    2.1) run_fun_change SGID_path.txt ;;
    2.2) run_fun_change SUID_path.txt ;;
    2.3) run_fun_change SUGID_path.txt ;;
    3.1) run_fun_see SUID_path.txt ;;
    3.2) run_fun_see SGID_path.txt ;;
    3.3) run_fun_see SUGID_path.txt ;;
    4.1) run_fun_see changed_SUID_file.txt ;;
    4.2) run_fun_see changed_SGID_file.txt ;;
    4.3) run_fun_see changed_SUGID_file.txt ;;
    *) echo "input error!! please input 0-8" ;;
    esac
    echo "press any key to continue......"
    read -p "$*"
    clear

done




